<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图 -->
    <el-card>
      <el-row :gutter="20">
        <!-- 搜索输入框 -->
        <el-col :span="10">
          <el-input
            clearable
            @clear="getGoodsList"
            placeholder="请输入内容"
            v-model="queryInfo.query"
            class="input-with-select"
          >
            <el-button
              slot="append"
              @clear="getGoodsList"
              icon="el-icon-search"
              @click="getGoodsList"
            ></el-button>
          </el-input>
        </el-col>

        <!--  添加商品按钮-->
        <el-col :span="5">
          <el-button  
           @click="goGoodsAdd"
           type="primary">添加商品</el-button>
        </el-col>
      </el-row>
      <!-- 商品表格 -->
      <el-table :data="goodsList" border>
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column label="商品名称" prop="goods_name"></el-table-column>
        <el-table-column label="商品价格" width="90px" prop="goods_price"></el-table-column>
        <el-table-column label="商品重量" width="90px" prop="goods_weight"></el-table-column>
        <el-table-column label="创建时间" width="90px" prop="add_time">
          <template slot-scope="scope">{{scope.row.add_time | dateFormat}}</template>
        </el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" icon="el-icon-edit">修改</el-button>
            <el-button type="danger" @click="removeGoods(scope.row)" size="mini" icon="el-icon-delete">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 20, 30]"
        :page-size="queryInfo.pagesize"
        background
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>
  </div>
</template>
<script>
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 5
      },
      goodsList: [],
      total: 0
    };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    // 获取商品数据 
    //找bug 可以log('123')确定属性是否收到
    // log('res')来确定是上面还是下面的问题
    //loa(res.data.goods)来确定 /  getGoodsList可能有问题
    async getGoodsList() {

      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo
      });
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error("获取数值失败");
      this.goodsList = res.data.goods;
      this.total = res.data.total;
    },
    // 切换每页发生变化
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    //页码发生变化
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },

    removeGoods(row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then( async() => {
           const {data:res}=await   this.$http.delete(`goods/${row.goods_id}`);
           console.log(res);
           
            if(res.meta.status !==200) return this.$message.error('删除失败')      
          this.$message.success("删除成功");
          this.getGoodsList();
        })
        .catch(() => {
          this.$message.error("已取消删除");
        });
    },
    goGoodsAdd(){
       this.$router.push('goods/add')
    }

  }
};
</script>
<style scoped lang="less">
</style>